<?php
/**
 * Created by XM·Tan.
 * Motto: Who shuts love out, in turn shall be shut out from love.
 * Date: 2021/3/22 18:18
 */

namespace app\admin\controller;

use app\admin\business\AdminUser as AdminUserBusiness;
use app\admin\validate\AdminUser as AdminUserValidate;
use app\common\lib\Show;
use think\captcha\facade\Captcha;
use think\facade\View;

class Login extends AdminBase {

    protected function initialize() {
        if ($this->isLogin()) {
            return $this->redirect(url('index/index'));
        }
    }

    // 显示验证码
    public function verify() {
        return Captcha::create();
    }

    // 登录页
    public function index() {
        return View::fetch();
    }

    // 验证登录信息
    public function check() {

        if (!$this->request->isPost()) {
            return Show::error('非法请求');
        }

        $data = input('param.');
        $validate = new AdminUserValidate();
        if (!$validate->scene('login')->check($data)) {
            return Show::error($validate->getError());
        }

        // 调用 business 层进行 登录逻辑和数据 操作
        try {
            $result = AdminUserBusiness::login($data);
        } catch (\Exception $e) {
            return Show::error($e->getMessage());
        }

        if ($result) {
            return Show::success('登录成功');
        } else {
            return Show::error('登录失败');
        }
    }

}